package com.ruoyi.event.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.event.domain.bo.EventPosBo;
import com.ruoyi.event.domain.vo.EventPosVo;
import com.ruoyi.event.service.IEventPosService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 用户定位
 *
 * @author strongme
 * @date 2023-04-05
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/event/pos")
public class EventPosController extends BaseController {

    private final IEventPosService iEventPosService;

    /**
     * 查询用户定位列表
     */
    @GetMapping("/list")
    public TableDataInfo<EventPosVo> list(EventPosBo bo, PageQuery pageQuery) {
        return iEventPosService.queryPageList(bo, pageQuery);
    }

    /**
     * 获取用户位置信息
     * @param bo
     * @return
     */
    @GetMapping("/listAll")
    public R<List<EventPosVo>> list(EventPosBo bo) {
        return R.ok(iEventPosService.queryList(bo));
    }

    /**
     * 导出用户定位列表
     */
    @Log(title = "用户定位", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(EventPosBo bo, HttpServletResponse response) {
        List<EventPosVo> list = iEventPosService.queryList(bo);
        ExcelUtil.exportExcel(list, "用户定位", EventPosVo.class, response);
    }

    /**
     * 获取用户定位详细信息
     *
     * @param id 主键
     */
    @GetMapping("/{id}")
    public R<EventPosVo> getInfo(@NotNull(message = "主键不能为空")
                                 @PathVariable Long id) {
        return R.ok(iEventPosService.queryById(id));
    }

    /**
     * 新增用户定位
     */
    @Log(title = "用户定位", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody EventPosBo bo) {
        if (bo.getUserId() == null) {
            bo.setUserId(LoginHelper.getUserBusinessId());
        }
        return toAjax(iEventPosService.insertByBo(bo));
    }

    /**
     * 修改用户定位
     */
    @Log(title = "用户定位", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody EventPosBo bo) {
        return toAjax(iEventPosService.updateByBo(bo));
    }

    /**
     * 删除用户定位
     *
     * @param ids 主键串
     */
    @Log(title = "用户定位", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iEventPosService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
